package pref.junx.flybird.activity.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import pref.junx.flybird.R;
import pref.junx.flybird.activity.UserInfo.UserInfoActvity;
import pref.junx.flybird.activity.addFri.Addfri_Actvity;
import pref.junx.flybird.activity.addevent.AddEventActvity;
import pref.junx.flybird.activity.note.NoteActvity;
import pref.junx.flybird.base.BaseActicvitylhy;
import pref.junx.flybird.fragment.daily.Group.GroupFragment;
import pref.junx.flybird.fragment.daily.event.DailyFragment;
import pref.junx.flybird.fragment.daily.fri.FriFragment;
import pref.junx.flybird.tools.JumpUtil;

/**
 * Created by 半生瓜 on 2017/7/20.
 */

public class NewMainActivity extends BaseActicvitylhy<MainPresenter, MainModel> implements MainContract.view {
    TextView nick,nub;
    public static NewMainActivity newMainActivity;
    TabLayout main_tablayout;
    Toolbar main_toolbar;
    ActionBarDrawerToggle actionBarDrawerToggle;
    private NavigationView navigationView;
     View linearLayout;
    private FragmentPagerAdapter adapter;
    private View hearlayout;
    //todo 三个tab对象
    FloatingActionButton fab;
    ViewPager main_viewPager;
    DrawerLayout drawer;
    private String[] tabTitles = {"事件", "联系人", "群组"};
    private int[] imageResId = {R.drawable.ic_timeline_white_18dp, R.drawable.ic_person_black_24dp, R.drawable.ic_favorite_black_20dp};
    int code;
    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initview() {
        fab=(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(this);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        linearLayout=navigationView.inflateHeaderView(R.layout.nav_header_main);
        nick=(TextView)linearLayout.findViewById(R.id.nick) ;
        nub=(TextView)linearLayout.findViewById(R.id.name) ;
        newMainActivity=this;

        main_toolbar = (Toolbar) findViewById(R.id.toolbar);
        main_viewPager = (ViewPager) findViewById(R.id.viewpager);
        main_tablayout = (TabLayout) findViewById(R.id.tab_layout);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//        navigationView= (NavigationView) findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, main_toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        //todo drawerlayout布局侧滑栏按键
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_note:
                       JumpUtil.to(c, NoteActvity.class);break;
                    case  R.id.nav_set:
                        JumpUtil.to(c, UserInfoActvity.class);break;

                }
                return true;
            }
        });

    }

    @Override
    public void init() {
        code=spTools.getInt("code");
        nick.setText(spTools.getString("user_nick"));
        nub.setText(spTools.getString("user_name"));
        main_viewPager.setAdapter(adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return new DailyFragment();
                    case 1:
                        return new FriFragment();
                    case 2:
                        return new GroupFragment();
                }
                return null;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Drawable image = getResources().getDrawable(imageResId[position]);
                image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
                SpannableString sb = new SpannableString("   " + tabTitles[position]);
                ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
                sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;
            }
        });

        main_tablayout.setupWithViewPager(main_viewPager);
        main_tablayout.setTabMode(TabLayout.MODE_FIXED);
        if(code!=0){
            main_viewPager.setCurrentItem(code);
            spTools.deleteKey("code");
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    // TODO: 2017/8/7 初始化tab标签
    private void setupTabIcons() {

    }

    @Override
    public void processClick(View view) {
        switch (view.getId()) {
            case R.id.hear_nav:
                JumpUtil.to(c, UserInfoActvity.class);break;
            case R.id.fab:
                switch (main_viewPager.getCurrentItem()){
                    case 0:JumpUtil.to(c, AddEventActvity.class);break;
                    case 1:JumpUtil.to(c, Addfri_Actvity.class);break;
                }
        }
    }

    // todo 右上角选项按键设置
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.activity_sidebar_drawer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()){
//            case R.id.nav_schedule:
//                main_viewPager.setCurrentItem(0);
//                break;
//            case R.id.nav_report:
//                main_viewPager.setCurrentItem(1);
//                break;
//            case R.id.nav_history:
//                main_viewPager.setCurrentItem(2);
//                break;
//            case R.id.nav_manage:
//                break;
//            case R.id.nav_share:
//                JumpUtil.toLogin(c);
//                finish();
//                break;
//            case R.id.nav_send:
//                System.exit(0);
//                break;
//        }
        return true;
    }

}